<template>
  <div class="login-wrapper">
    <div class="container">
      <form @submit.prevent="handleLogin">
        <div class="card">
          <div class="login-box">
            <h1>登录</h1>

            <div class="form-group">
              <input
                  type="text"
                  id="username"
                  v-model="loginForm.name"
                  placeholder="用户名"
                  required
              >
            </div>

            <div class="form-group">
              <input
                  type="password"
                  id="password"
                  v-model="loginForm.pwd"
                  placeholder="密码"
                  required
              >
            </div>
            <div class="button-container">
              <button type="submit" class="login-btn">登 录</button>
              <router-link to="/register" class="login-btn">注 册</router-link>
            </div>
          </div>
        </div>
      </form>
    </div>
  </div>
</template>

<script>
import { reactive, ref } from "vue";
import { useRouter } from "vue-router";
import { Login } from "@/api";
import { useStore } from "vuex";

export default {
  name: "LoginPage",
  setup() {
    const router = useRouter();
    const store = useStore();
    const loginForm = reactive({
      name: '',
      pwd: ''
    });
    const isSubmitting = ref(false);
    const errorMessage = ref('');

    const handleLogin = async () => {
      // 前端简单验证
      if (!loginForm.name.trim() || !loginForm.pwd.trim()) {
        errorMessage.value = '用户名和密码不能为空';
        return;
      }

      isSubmitting.value = true;
      errorMessage.value = '';

      try {
        const response = await Login(loginForm);

        if (response.data.code !== 200) {
          alert(response.data?.data?.errorMsg || response.data?.errorMsg || '登录失败');
          isSubmitting.value = false;
          return;
        }

        // 存储token和用户信息
        const token = response.data.data.token;
        if (token) {
          localStorage.setItem('token', token);
          localStorage.setItem('type', response.data.data.type);
          localStorage.setItem('username', response.data.data.userName);
          localStorage.setItem('userId', response.data.data.userId);
        } else {
          alert('token格式不规范');
        }

        // 登录成功处理
        await store.dispatch('login', { username: loginForm.name });

        // 根据用户类型跳转不同页面
        if (response.data.data.type === 0) {
          await router.push("/manage/user");
        } else {
          await router.push("/tasteHome");
        }

      } catch (error) {
        console.error('登录错误:', error);
        errorMessage.value = '请求失败，请稍后再试';
        isSubmitting.value = false;
      }
    };

    return {
      loginForm,
      isSubmitting,
      errorMessage,
      handleLogin
    };
  }
};
</script>

<style scoped>
.login-wrapper {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-image: url('../assets/bg2.jpg');
  background-repeat: no-repeat;
  overflow: hidden;
  background-size: 100%;
  background-attachment: fixed;
}

.container {
  text-align: center;
  margin-top: 90px;
  width: 400px;
  height: 500px;
  padding: 60px 0;
  background-color: rgba(255, 255, 255, 0.2);
  backdrop-filter: blur(10px);
  position: relative;
  border-radius: 20px;
}

.card {
  padding: 20px;
}

.login-box h1 {
  font-size: 50px;
  cursor: pointer;
  color: rgba(42, 130, 148);
  margin-bottom: 30px;
}
.form-group {
  margin-bottom: 20px;
  position: relative;
}
.form-group input {
  width: 250px;
  height: 40px;
  border-radius: 5px;
  margin-bottom: 10px;
  padding-left: 15px;
  outline-style: none;
  border: 1px solid #ccc;
}

.form-group input:hover {
  box-shadow: 2px 2px 5px #555;
  background-color: #ddd;
}

.button-container {
  display: flex;
  justify-content: center;
  gap: 10px;
  margin-top: 30px;
}

.login-btn {
  width: 125px;
  height: 40px;
  background-color: rgba(42, 130, 148);
  cursor: pointer;
  border: none;
  border-radius: 5px;
  color: white;
  font-size: 16px;
  text-decoration: none;
  display: flex;
  align-items: center;
  justify-content: center;
}

.login-btn:hover {
  background-color: rgba(21, 113, 150);
}

.error-message {
  color: red;
  font-size: 14px;
  margin-top: 5px;
}

@media (max-width: 576px) {
  .container {
    width: 90%;
    height: auto;
    padding: 50px 20px;
    margin-top: 50px;
  }

  .form-group input {
    width: 90%;
  }

  .button-container {
    flex-direction: column;
    align-items: center;
  }

  .login-btn {
    width: 90%;
    margin-bottom: 10px;
  }
}
</style>
